package cn.yup.service.impl;

import cn.yup.domain.Role;
import cn.yup.mapper.RoleMapper;
import cn.yup.query.QueryObject;
import cn.yup.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired //  表示 从spring 容器中 获取 DepartmentMapper 对象 赋给 departmentMapper 这个变量
    private RoleMapper roleMapper  ;


    @Override
    public PageInfo findAll(QueryObject queryObject) {

        PageHelper.startPage(queryObject.getCurrentPage(),queryObject.getPageSize());
        // 当前页的数据
        List <Role> roles =  roleMapper.findAll();
        return  new PageInfo(roles);
    }

    @Override
    public void deleteById(Long id) {
        roleMapper.deleteById(id);
    }

    @Override
    public Role findById(Long id) {
        return roleMapper.findById(id);
    }

    @Override
    public void saveOrUpdate(Role role,Long[] ids) {
        if (role.getId() != null) {
            roleMapper.deleteRelation(role.getId());
            roleMapper.updateById(role);
        }
        else {

            roleMapper.save(role);
        }
        if(ids !=null){
            for (Long pId : ids){
                roleMapper.insertRelation(role.getId(),pId);
            }
        }


    }

    @Override
    public List<Role> listAll() {
        return roleMapper.findAll();
    }
}
